#include "DashboardUI_PatientAdd.h"
#include "ui_dashboard_patientadd.h"

#include "Authentication.h"

#include "PatientListModel.h"
#include "UserRelationDatabase.h"

DashboardUI_PatientAdd::DashboardUI_PatientAdd(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DashboardUI_PatientAdd)
{
    ui->setupUi(this);

    model.loadAll();
    ui->patientListView->setModel(&model);
}

DashboardUI_PatientAdd::~DashboardUI_PatientAdd()
{
    delete ui;
}

void DashboardUI_PatientAdd::on_cancelButton_clicked()
{
    deleteLater();
}

void DashboardUI_PatientAdd::on_addButton_clicked()
{
    QModelIndex index = ui->patientListView->currentIndex();

    if (!index.isValid())
        return;

    int id = index.data(Qt::UserRole).toInt();

    UserRelationDatabase relationDB;
    Authentication * auth = Authentication::getInstance();

    relationDB.addRelation(auth->getUser()->getId(), id);

    emit patientAdded();

    deleteLater();
}
